package cn.fl.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cmm.comm.facade.bank.api.ICmmBankNoApiService;
import cmm.comm.facade.bank.condition.CmmBankNoQC;
import cn.fl.system.api.ISysSequenceApiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.api.ISysEmpBankAccApiService;
import cmm.user.facade.system.condition.SysEmpBankAccQueryCondition;
import cmm.user.facade.system.dto.SysEmpBankAccDTO;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.custpay.condition.CmmBankNoQueryCondition;
import cn.fl.custpay.service.ICmmOlWithholdingApiService;
import cn.fl.custpay.transform.CmmQcbankDetVOTransform;
import cn.fl.custpay.transform.CmmRealAuthVOTransform;
import cn.fl.custpay.vo.CmmQcbankDetVO;
import cn.fl.custpay.vo.CmmRealAuthVO;
import cn.fl.framework.base.constant.WebBaseConstant;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.system.service.ISysEmpBankAccService;
import cn.fl.system.transform.CmmSignedBankVOTransform;
import cn.fl.system.transform.SysEmpBankAccVOTransform;
import cn.fl.system.vo.CmmSignedBankVO;
import cn.fl.system.vo.SysEmpBankAccVO;

/**
 * @Description: 员工银行账户信息表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class SysEmpBankAccServiceImpl implements ISysEmpBankAccService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(ISysEmpBankAccService.class);
	
	/**
	 * TODO 自定义 API 暴露服务
	 */
	@Autowired
	private ISysEmpBankAccApiService sysEmpBankAccApiService;

	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

	@Autowired
	private ISysSequenceApiService sysSequenceApiService;

	@Autowired
	private ICmmOlWithholdingApiService cmmOlWithholdingApiService;

	@Autowired
	private ICmmBankNoApiService cmmBankNoApiService;

	@Override
	public SysEmpBankAccVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//TODO 业务逻辑
		SysEmpBankAccDTO dto = sysEmpBankAccApiService.selectById(id);
		return SysEmpBankAccVOTransform.toVO(dto);
	}

	@Override
	public Long add(SysEmpBankAccVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		return this.sysEmpBankAccApiService.add(SysEmpBankAccVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return this.sysEmpBankAccApiService.removeById(id);
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(SysEmpBankAccVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return this.sysEmpBankAccApiService.modify(SysEmpBankAccVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page) return new Pager();
		Pager resultpager = this.sysEmpBankAccApiService.searchListPage(page);
		List<SysEmpBankAccVO> list = SysEmpBankAccVOTransform.toVOList((List<SysEmpBankAccDTO>)resultpager.getResultList());
		resultpager.setResultList(list);
		return resultpager;
	}

	@Override
	public List<KeyValueVO> searchBankNmList() throws BaseException{
		List<KeyValueVO> banklist = new ArrayList<KeyValueVO>();
		List<SysDictionaryDTO>  sysdto = this.sysDictionaryApiService.searchGroup(CurrentThreadContext.getFlServiceSysCd(),CurrentThreadContext.getCurrentOrgCd(),"CST0014");
		if(CollectionUtils.isNotEmpty(sysdto)){
			for(SysDictionaryDTO dto : sysdto){
				KeyValueVO keyValue = new KeyValueVO();
				keyValue.setKey(dto.getCodeAttr1());
				keyValue.setValue(dto.getCodeNm());
				banklist.add(keyValue);
			}
		}
		return banklist;
	}

	@Override
	public Map<String, String> searchGroupToMap(String sysCd, String orgCd, String codeGrpCd) throws BaseException {
		return this.sysDictionaryApiService.searchGroupToMap(sysCd, orgCd, codeGrpCd);
	}

	@Override
	public List<SysEmpBankAccVO> searchList(SysEmpBankAccQueryCondition condition) throws BaseException {
		List<SysEmpBankAccVO> volist = new ArrayList<SysEmpBankAccVO>();
    	if(null == condition) return volist;
		List<SysEmpBankAccDTO> dtolist= this.sysEmpBankAccApiService.searchList(condition);
		if(CollectionUtils.isNotEmpty(dtolist)){
			volist = SysEmpBankAccVOTransform.toVOList(dtolist);
		}
		return volist;
	}
	//加入通过用户userid查询数据
	@Override
	public List<SysEmpBankAccVO> selectEmpBankList(SysEmpBankAccQueryCondition condition) throws BaseException {
		List<SysEmpBankAccVO> volist = new ArrayList<SysEmpBankAccVO>();
    	if(null == condition) return volist;
		List<SysEmpBankAccDTO> dtolist= this.sysEmpBankAccApiService.searchEmpBankList(condition);
		if(CollectionUtils.isNotEmpty(dtolist)){
			volist = SysEmpBankAccVOTransform.toVOList(dtolist);
		}
		return volist;
	}

	@Override
	public Long getFun(String funNm) throws BaseException {
		if(StringUtils.isEmpty(funNm)){
			return null;
		}
		return this.sysSequenceApiService.getFun(funNm);
	}

	@Override
	public Long addCmmRealAuth(CmmRealAuthVO entity) throws BaseException {
		if(null == entity){
			BaseLogUtils.info(logger, "addCmmRealAuth", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		return this.cmmOlWithholdingApiService.addCmmRealAuth(CmmRealAuthVOTransform.toDTO(entity));
	}

	@Override
	public List<SysEmpBankAccVO> searchEmpBankList(SysEmpBankAccQueryCondition condition) throws BaseException {
		List<SysEmpBankAccVO> volist = new ArrayList<SysEmpBankAccVO>();
		if(null == condition) return volist;
		List<SysEmpBankAccDTO> dtolist= this.sysEmpBankAccApiService.searchEmpBankList(condition);
		if(CollectionUtils.isNotEmpty(dtolist)){
			volist = SysEmpBankAccVOTransform.toVOList(dtolist);
		}
		return volist;
	}

	@Override
	public String qcBankNo(CmmBankNoQC condition) throws BaseException {
		return this.cmmBankNoApiService.qcBankNo(condition);
	}

	@Override
	public Long addCmmSignedBank(CmmSignedBankVO entity) throws BaseException {
		if(null == entity){
			BaseLogUtils.info(logger, "addCmmSignedBank", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		return this.cmmOlWithholdingApiService.addCmmSignedBank(CmmSignedBankVOTransform.toDTO(entity));
	}
	
	@Override
	public Map<String, String> getBankNameMap() throws BaseException {
		Map<String, String> bankname = new HashMap<String, String>();
		List<SysDictionaryDTO>  sysdto = this.sysDictionaryApiService.searchGroup(CurrentThreadContext.getFlServiceSysCd(), WebBaseConstant.SYS_P_ORG_CD,"CST0014");
		for(SysDictionaryDTO dto : sysdto){
			bankname.put(dto.getCodeAttr1(),dto.getCodeNm());
		}
		bankname.put("ELSEB","其他银行");
		return bankname;
	}

	@Override
	public List<String> qctwobank(CmmBankNoQC condition) throws BaseException {
		return this.cmmBankNoApiService.qctwobank(condition);
	}
	@Override
	public Long addCmmQcbankDet(CmmQcbankDetVO entity) throws BaseException {
		if(null == entity){
			BaseLogUtils.info(logger, "addCmmQcbankDet", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		return this.cmmOlWithholdingApiService.addCmmQcbankDet(CmmQcbankDetVOTransform.toDTO(entity));
	}
	
	@Override
	public List<String> qcThreebank(CmmBankNoQC condition) throws BaseException {
		return this.cmmBankNoApiService.qcThreebank(condition);
	}

}
